package com.gryffonx.utils;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.Interfaces.generators.NameGeneratorsBinding;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.tree.DefaultElement;

import java.util.List;


/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Sep 10, 2004
 * Time: 4:59:22 PM
 * Process:
 * **************************************************************************
 */
public class XMLUtils {

    public static String getNodeValue(String type, Document xmlDoc, int groupNumber, NameGeneratorsBinding parent) {
        String retVal = "";
        List nodes = xmlDoc.selectNodes("/generator/" + type + "Elements/" + type + "[@" + type + "_id='" + groupNumber + "']/" + type + "_element");
        int howMany = nodes.size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            DefaultElement element = (DefaultElement)nodes.get(roll-1);
            retVal = element.getText();
        }

        parent.getTextPane1().setText(parent.getTextPane1().getText() + "Rolled a "+roll+" from "+howMany+" possible options, with an outcome of " + retVal + "\r\n");
        return retVal.toLowerCase();
    }

    public static String getNodeValue(String type, Document xmlDoc, int groupNumber) {
        String retVal = "";
        List nodes = xmlDoc.selectNodes("/generator/" + type + "Elements/" + type + "[@" + type + "_id='" + groupNumber + "']/" + type + "_element");
        int howMany = nodes.size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            DefaultElement element = (DefaultElement)nodes.get(roll-1);
            retVal = element.getText();
        }

        return retVal.toLowerCase();
    }

    public static String getNodeValue(Document xmlDoc, String groupName, NameGeneratorsBinding parent) {
        String retVal = "";
        List nodes = xmlDoc.selectNodes("/generator/" + "suffix" + "Elements/" + "suffix" + "[@" + "suffix" + "_type='" + groupName + "']/" + "suffix" + "_element");
        int howMany = nodes.size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            DefaultElement element = (DefaultElement)nodes.get(roll-1);
            retVal = element.getText();
        }
        parent.getTextPane1().setText(parent.getTextPane1().getText() + "Rolled a "+roll+" from "+howMany+" possible options, with an outcome of " + retVal + "\r\n");
        return retVal.toLowerCase();
    }

    public static String getNodeValue(Document xmlDoc, String groupName) {
        String retVal = "";
        List nodes = xmlDoc.selectNodes("/generator/" + "suffix" + "Elements/" + "suffix" + "[@" + "suffix" + "_type='" + groupName + "']/" + "suffix" + "_element");
        int howMany = nodes.size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            DefaultElement element = (DefaultElement)nodes.get(roll-1);
            retVal = element.getText();
        }

        return retVal.toLowerCase();
    }
    public static String getMatrixValue(Document xmlDoc, int nodeNumber) {
        String retVal = "";
        Node thisNode;


        Object[] trainingClassArray = xmlDoc.selectNodes("/generator/prefixElements/prefix" +
                "/prefix_element[@prefix_element_id='" + nodeNumber + "']").toArray();
            thisNode = (Node) trainingClassArray[0];
            retVal = thisNode.getStringValue();


        return retVal.toLowerCase();

    }
}
